package net.starelement.digwartnt.game;

import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.event.EventHandler;
import cn.nukkit.event.entity.EntityDamageByEntityEvent;
import cn.nukkit.event.player.PlayerInteractEntityEvent;
import cn.nukkit.event.player.PlayerQuitEvent;
import net.starelement.digwartnt.DigwarTNT;
import net.starelement.digwartnt.task.CountdownTask;
import net.starelement.gamelib.player.GamePlayerList;

/**  StarElement DigwarTNT 游戏准备类
 *
 * @author Ruok233
 * @since NukkitX API 1.0.9 | StarElement Project v1-alpaca
 *
 * create: 2019/12/05
 */
public class GameReady extends net.starelement.gamelib.game.GameReady {

    public static GamePlayerList list = new GamePlayerList();

    public GameReady() {
        super();
    }

    public GameReady(int count) {
        super(count);
    }

    @Override
    public void run() {
        super.server.broadcastMessage("§a§l游戏即将开始");
        super.scheduler.scheduleRepeatingTask(new CountdownTask(DigwarTNT.getInstance(), 10), 20);
    }

    /*
    临时入口
     */
    @EventHandler
    public void de(PlayerInteractEntityEvent event) {
        if (event.getEntity().getName().equals("Villager")) {
            ready(event.getPlayer());
        }
    }

    @EventHandler
    public void d(EntityDamageByEntityEvent event) {
        if (event.getEntity().getName().equals("Villager")) {
            event.setCancelled();
            String name = event.getDamager().getName();
            Player player = Server.getInstance().getPlayer(name);
            ready(player);
        }
    }

    @EventHandler
    public void ddd(PlayerQuitEvent event) {
        list.remove(event.getPlayer());
        server.broadcastMessage("§e" + event.getPlayer().getName() + " §a离开了！§b(" + list.size()+ "/" + super.count + ")");
    }

    private void ready(Player player) {
        list.add(player);
        server.broadcastMessage("§e" + player.getName() + " §a已经准备好了！§b(" + list.size()+ "/" + super.count + ")");
        if (list.size() == super.count) {
            list.clear();
            run();
        }
    }
}
